import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Link from '../views/Link.vue'
import Mine from '../views/Mine.vue'
import Look from '../views/Look.vue'
import New from '../views/New.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/link',
    name: 'link',
    component: Link,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/mine',
    name: 'mine',
    component: Mine,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/look',
    name: 'look',
    component: Look,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/new',
    name: 'new',
    component: New,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/login',
    name: 'login',
    component:() => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component:() => import('../views/Register.vue')
  },
  {
    path: '/chat/:id',
    name: 'chat',
    component:() => import('../views/Chat.vue'),
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/friend/:id',
    name: 'friend',
    component:() => import('../views/Friend.vue'),
    meta: {
      requireAuth: true
    }
  },
]


const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  // window.document.title=to.meta.title;
 if(to.meta.requireAuth == true){  //需要登录权限进入的路由
   if(!localStorage.getItem('userInfo')){  //取不到登录信息
      return next({name:'login'})
   }else{  //取得到登录信息，进行下一步
     return next();
   }
 }else{  //不需要登录权限
   return next();
 }
})

export default router
